package com.palm.hubo2o.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.palm.hubo2o.R;
import com.palm.hubo2o.model.Bet;
import com.palm.hubo2o.util.BallUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dest1 on 2015/10/12.
 */
public class BetAdapter extends RecyclerView.Adapter<BetAdapter.BetHolder> {

    private ArrayList<Bet> bets;

    public BetAdapter() {
        super();
        bets = new ArrayList<>();
    }

    public void addOrder(@NonNull Bet[] bet) {
        bets.addAll(Arrays.asList(bet));
        notifyItemRangeInserted(bets.size() - bet.length, bet.length);
    }


    public void addOrder(@NonNull Bet bet) {
        bets.add(bet);
        notifyItemInserted(bets.size() - 1);
    }


    public ArrayList<Bet> getBets() {
        return bets;
    }

    @Override
    public BetHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bet, parent, false);
        return new BetHolder(v);
    }

    @Override
    public void onBindViewHolder(BetHolder holder, int position) {
        Bet o = bets.get(position);
        holder.tvBalls.setText(BallUtils.coloredOrder(o));
        long note = o.getWagerCount();
        holder.tvNote.setText(String.format(Locale.getDefault(), "%s%d注%d元", o.getPlayType().getTypeName(), note, note * 2));
    }

    @Override
    public int getItemCount() {
        if (bets == null)
            return 0;
        return bets.size();
    }


    public void cleanUp() {
        bets.clear();
        notifyDataSetChanged();
    }

    class BetHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.balls_item_order)
        TextView tvBalls;
        @Bind(R.id.note_item_order)
        TextView tvNote;

        public BetHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
